/**
 * @file 预约时间
 * @author yangjiangming <449654337@qq.com>
 * @date 2018/12/14 16:38:57
 */

layui.define(['layer', 'form', '$http', 'zmnOmsSelect'], function (exports) {
    var $ = layui.$;
    var $http = layui.$http;
    var layer = layui.layer;
    var form = layui.form;
    var zmnOmsSelect = layui.zmnOmsSelect;

    // url
    var LIST_COMPANY_URL = '/company/list.action';
    var LIST_MANAGE_COMPANY_URL = '/company/manage/company/list.action';
    var CHECK_COMPANY_URL = '/company/check.action';

    var options;

    /**
     * 服务商下拉框
     * @param companyId
     */
    function selectManageCompany(companyId) {
        zmnOmsSelect.selectManageCompany('manageCompanyId', {
            tips: "-选择-",   //默认提示
            type: 1,
            events: {  //事件配置
                /**
                 * 选择中事件
                 * @param values 已选中值，Array类型
                 */
                changed: function (values) {
                    let companyId = $("#companyId").val()
                    if(!companyId){
                        layer.msg("请先选择服务商子公司！")
                        zmnOmsSelect.clearSelectedData('manageCompanyId')
                        return;
                    }
                }
                },
        });
    }

    /**
     * 初始化子公司
     */
    var initCompanySelect = (function () {
        /**
         * 根据平台查询子公司
         * @param params 查询参数
         * @returns {*}
         * @private
         */
        function _fetchCompanyByPlat (params) {
            return $http.$get(LIST_COMPANY_URL, params);
        }

        /**
         * 渲染子公司下拉框
         */
        function _renderCompanySelect (data) {
            var htmlStr = '<option value="">-选择-</option>';
            data.forEach(function (item) {
                htmlStr += '<option value="' + item.value + '">' + item.text + '</option>';
            });

            var $companyId = $('#companyId');

            if ($companyId.length) {
                $companyId.html(htmlStr);
                form.render();
            }
        }

        return function (plat) {
            if (!plat) {
                _renderCompanySelect([]);
            }

            _fetchCompanyByPlat({ plat: plat }).then((data) => {
                _renderCompanySelect(data);
            }, () => {
                _renderCompanySelect([]);
            });
        }
    })();

    /**
     * 初始化页面
     */
    function initPage () {
        options = parent.window.checkCompanyOptions;
        selectManageCompany();
    }

    function initEvent() {

        form.render();

        // 监听平台下拉框事件
        form.on('select(select-plat)', function (data) {
            var value = data.value;
            initCompanySelect(value);
        });

        // 监听子公司下拉框事件
        form.on('select(select-company)', function (data) {
            // 每次选择子公司时，清空【服务商下拉框】内容
            zmnOmsSelect.clearSelectedData('manageCompanyId')
            if(!data.value){
                selectManageCompany();
                return;
            }
            selectManageCompany(data.value);
        });

        /**
         * 校验按钮
         */
        form.on('submit(checkCompany)', function (data) {
            if(!data.field.manageCompanyId || data.field.manageCompanyId === ''){
                layer.msg('请选择服务商');
                return false;
            }

            var params = {
                workId: options.workId,
                reworkId: options.reworkId,
                type: options.type,
                recommenderId: options.recommenderId,
                channelId: options.channelId,
                platWork: options.platWork,
                plat: $("#plat").val(),
                bizType: options.bizType,
                cityId: options.cityId,
                productId: options.productId,
                longitude: options.longitude,
                latitude: options.latitude,
                streetId: options.streetId,
                countyId: options.countyId,
                dutyTime: options.dutyTime,
                preManageCompanyId: data.field.manageCompanyId
            };

            $http.$get(CHECK_COMPANY_URL, params).then(function (data) {
                if (data == 1){
                    $("#checkResult").text('自动分单满足').css("color", "#00CC00");
                } else if (data == 2) {
                    $("#checkResult").text('人工分单满足').css("color", "#000000");
                }
            }, function (msg) {
                $("#checkResult").text(msg).css("color", "#FF0000");
            });

            return false;
        });

    }

    function init () {
        initPage();

        initEvent();
    }

    init();

    exports("oms_order_checkCompany");
});
